﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Product;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class DinnerEdit : UserControl
    {
        public ProductLogic LgcProduct;
        public DinnerLogic LgcDinner;
        public List<V_DinnerItem> DinnerItems;
        public T_Dinner Tdinner;
        public V_Dinner Vdinner;
        public ComboLogic LgcCombo;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public DinnerEdit()
        {
            InitializeComponent();
            LgcProduct = new ProductLogic();
            DinnerItems = new List<V_DinnerItem>();
            Tdinner = new T_Dinner();
            LgcDinner = new DinnerLogic();
            LgcCombo = new ComboLogic();

            cmbState.Text = "可办理";

            grdProduct.AutoGenerateColumns = false;
            grdProduct.DataSource = LgcProduct.GetProdutAndServiceView();
            lblProductCount.Text = grdProduct.RowCount.ToString();

            grdDinnerItem.AutoGenerateColumns = false;
            lblItemCount.Text = grdDinnerItem.RowCount.ToString();
            //grdDinnerItem.DataSource = DinnerItems;

            var brokerageTypes = LgcCombo.GetComboByBuzType("ProjectDinnerItemType").ToList();
            //dgvCmbType.DataSource = brokerageTypes;
            //dgvCmbType.DisplayMember = "DisplayText";
            //dgvCmbType.ValueMember = "Value1";
            //if (brokerageTypes.Any())
            //    dgvCmbType.DefaultCellStyle.NullValue = brokerageTypes[0].DisplayText;

            var stores = LgcCombo.GetComboByBuzType("DinnerItemLimitType").ToList();
            //dgvCmbTimeLimitType.DataSource = stores;
            //dgvCmbTimeLimitType.DisplayMember = "DisplayText";
            //dgvCmbTimeLimitType.ValueMember = "Value1";
            //if (stores.Any())
            //    dgvCmbTimeLimitType.DefaultCellStyle.NullValue = stores[0].DisplayText;

            SPACommon.SetDataGridStyle(grdProduct);
            SPACommon.SetDataGridStyle(grdDinnerItem);



        }


        private void DinnerEdit_Load(object sender, EventArgs e)
        {
            txtName.Focus();
        }

        public void UiSetValue(V_Dinner dinner, List<V_DinnerItem> dinnerItems)
        {
            grdDinnerItem.DataSource = null;
            grdDinnerItem.DataSource = dinnerItems;
            txtName.Text = dinner.Name;
            txtTreeName.Text = dinner.TreeName;
            numPrice.Value = dinner.Price;
            cmbState.Text = dinner.State;
            txtEffectTime.Text = dinner.EffectTime.ToString();
            cmbEffectType.Text = dinner.EffectType;
            numMark.Value = dinner.Mark;
            txtRemark.Text = dinner.Remark;
            numTimes.Value = dinner.Times;
        }

        public void GetDinner(string id)
        {
            Vdinner = LgcDinner.GetDinnerViewObject(" where T_Dinner.Id= '" + id + "'");
            DinnerItems = LgcDinner.GetDinnerItemView(" where T_DinnerItem.DinnerId = '" + id + "'");
            if (DinnerItems == null) DinnerItems = new List<V_DinnerItem>();
            //Tbill.BillNo = LgcBill.GetCountNo("T_Bill", true);
            UiSetValue(Vdinner, DinnerItems);
            Tdinner.Id = Vdinner.Id;
            //Tbill.MemberId = Vmember.Id;
            //Tbill.ShopId = S_Admin.ShopId;
        }

        private void txtProductSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
            {
                if (grdProduct.RowCount == 0) return;
                int index, move = 1;
                if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) move = -1;
                if (grdProduct.CurrentRow == null) index = 0;
                else if (grdProduct.CurrentRow.Index + move >= grdProduct.RowCount || grdProduct.CurrentRow.Index + move < 0)
                    index = 0;
                else index = grdProduct.CurrentRow.Index + move;
                if ((e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) && index == 0) index = 0;
                grdProduct.Rows[index].Selected = true; //选中结果所在的行
                grdProduct.CurrentCell = grdProduct.Rows[index].Cells[0];
                return;
            }

            if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Enter)
            {
                if (rdBarCode.Checked)
                {
                    var barCode = txtProductSearch.Text;
                    grdProduct.AutoGenerateColumns = false;
                    grdProduct.DataSource = null;
                    string whereBarCode = string.Format(" where E.BarCode like '%{0}%' ", barCode);
                    grdProduct.DataSource = LgcProduct.GetProdutView(whereBarCode);
                }
                btnAddItem_Click(null, null);
                if (txtProductSearch.Text.Length > 0) txtProductSearch.Text = txtProductSearch.Text.Substring(0, txtProductSearch.Text.Length - 1);
                txtProductSearch.Text = "";
                txtProductSearch.Focus();
                return;
            }
            if (e.KeyCode == Keys.Subtract)
            {
                btnDeleteItem_Click(null, null);
                if (txtProductSearch.Text.Length > 0) txtProductSearch.Text = txtProductSearch.Text.Substring(0, txtProductSearch.Text.Length - 1);
                return;
            }
            var search = txtProductSearch.Text;
            grdProduct.AutoGenerateColumns = false;
            grdProduct.DataSource = null;
            grdProduct.DataSource = LgcProduct.GetProdutView(" where p.Name like '%" + search + "%' or T.Name like '%"+search+"%'");

            lblProductCount.Text = grdProduct.RowCount.ToString();
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (grdProduct.CurrentRow == null) return;
            var id = grdProduct.CurrentRow.Cells["Id"].Value.ToString();
            var name = grdProduct.CurrentRow.Cells["ProductName"].Value.ToString();
            var treeName = grdProduct.CurrentRow.Cells["treeName"].Value.ToString();
            var salePrice = Convert.ToDecimal(grdProduct.CurrentRow.Cells["salePrice"].Value);

            if (DinnerItems == null) DinnerItems = new List<V_DinnerItem>();
            DinnerItems.Add(new V_DinnerItem
            {
                //Id = id,
                ObjectId = id,
                ItemName = name,
                Type = "计次",
                TimeLimitType = "无限期",
                LowestPrice = salePrice,
                Times = Convert.ToInt32(numTimes.Value),
                //Amount = Convert.ToDecimal(1.00),
                Discount = Convert.ToDecimal(1.00)
            });
            grdDinnerItem.DataSource = null;
            grdDinnerItem.DataSource = DinnerItems;
            lblItemCount.Text = grdDinnerItem.RowCount.ToString();
            //if (grdDinnerItem.Rows.Count > 0) { grdDinnerItem.CurrentCell = grdDinnerItem.Rows[0].Cells[3]; }
            //lblTotalMoney.Text = DinnerItems.Sum(m => m.Price * m.Amount).ToString();
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (grdDinnerItem.CurrentRow == null) return;
            var name = grdDinnerItem.CurrentRow.Cells["ItemName"].Value.ToString();
            grdDinnerItem.DataSource = null;
            var dinnerItem = DinnerItems.First(m => m.ItemName == name);
            DinnerItems.Remove(dinnerItem);
            //grdBillItem.CurrentRow
            grdDinnerItem.DataSource = DinnerItems;
            lblItemCount.Text = grdDinnerItem.RowCount.ToString();
            //grdBillItem.Rows.Remove(grdBillItem.CurrentRow);
            //lblTotalMoney.Text = grdDinnerItem.Sum(m => m.Price * m.Amount).ToString();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("DinnerTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            Tdinner.TreeId = formTreeSelecter.TypeId;
            txtTreeName.Text = formTreeSelecter.TypeText;
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {

            Tdinner.Name = txtName.Text;
            Tdinner.Price = numPrice.Value;
            Tdinner.State = cmbState.Text;
            //Tdinner.TreeId=txtTreeName.Text;
            //Tdinner.EffectTime = Convert.ToInt32(txtEffectTime.Text);
            //Tdinner.EffectType=cmbEffectType.Text;
            Tdinner.Mark = Convert.ToInt32(numMark.Value);
            Tdinner.Remark = txtRemark.Text;
            Tdinner.Times = Convert.ToInt32(numTimes.Value);

            Tdinner.Remark = txtRemark.Text.Trim();
            grdDinnerItem.EndEdit(DataGridViewDataErrorContexts.Commit);
            //dgvBorakerage.EndEdit(DataGridViewDataErrorContexts.Commit);
            //Borakerages = dgvBorakerage.DataSource;
            int row = LgcDinner.Save(Tdinner, DinnerItems);
            const string message = "保存失败";
            if (row == 0) FormSysMessage.ShowSuccessMsg(message);
            else MemberList.Instance.btnSearchDinner_Click(null, null);
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void dgvBorakerage_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //if (this.dgvBorakerage.CurrentCell.RowIndex > -1)
            //{
            //    if (e.Control is DataGridViewTextBoxEditingControl)
            //    {
            //        var editingControl = (DataGridViewTextBoxEditingControl)e.Control;
            //        //增加委托处理
            //        editingControl.KeyUp += new KeyEventHandler(this.EditingControl_KeyUp);
            //    }
            //    if (e.Control is DataGridViewComboBoxEditingControl)
            //    {
            //        var editingControl = (DataGridViewComboBoxEditingControl)e.Control;
            //        //增加委托处理
            //        editingControl.KeyUp += new KeyEventHandler(this.EditingControl_KeyUp);
            //    }
            //}
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }



    }
}
